# 展示了如何使用`__getattr__`、`__setattr__`和属性描述符来实现属性访问和动态属性。

# 1. 使用`__getattr__`获取动态属性：


class DynamicAttributes:
    def __getattr__(self, name):
        # 如果属性名是dynamic_attribute
        if name == "dynamic_attribute":
            return "这是个动态属性"
        else:
            # 否则抛出异常
            raise AttributeError(f"{name} is not found")

obj = DynamicAttributes()
print(obj.dynamic_attribute)  #
print(obj.undefined_attribute)

#  使用`__setattr__`设置动态属性：
class DynamicAttributes:
    def __setattr__(self, name, value):
        print(f"Setting {name} to {value}")
        super().__setattr__(name, value)

obj = DynamicAttributes()
obj.dynamic_attribute = 10  # 输出: Setting dynamic_attribute to 10